package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.SystemService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.SystemVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.SystemQueryForm;
import cn.icanci.loopstack.bic.web.mapper.SystemWebMapper;
import cn.icanci.loopstack.bic.web.model.System;

/**
 * 系统控制层Controller
 * 
 * @author icanci
 * @since 1.0 Created in 2023/09/10 15:16
 */
@RestController
@SuppressWarnings("all")
@RequestMapping("/bicAdmin/system")
public class SystemController {
    @Resource
    private SystemWebMapper systemWebMapper;
    @Resource
    private SystemService   systemService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody SystemQueryForm form) {
        return R.builderOk()
            .data("queryPage", systemService.queryPage(systemWebMapper.web2vo(form.getSystem()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize())).build();
    }

    @PostMapping("save")
    public R save(@RequestBody System system) {
        systemService.save(systemWebMapper.web2vo(system));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{name}")
    public R validateName(@PathVariable("name") String name) {
        SystemVO systemVO = systemService.queryByName(name);
        return R.builderOk().data("result", systemVO == null).build();
    }

    @GetMapping("validateKey/{key}")
    public R validateKey(@PathVariable("key") String key) {
        SystemVO systemVO = systemService.queryByKey(key);
        return R.builderOk().data("result", systemVO == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector")
    public R loadSelector() {
        List<TextValue> textValues = systemService.loadSelector();
        return R.builderOk().data("textValues", textValues).build();
    }

}
